import Vue from 'vue'
import VueRouter from 'vue-router'

const Login = () => import("views/Login") 
const Home = () => import("views/Home")
const Users = () => import("views/users/Users")
const Roles = () => import("views/power/Roles")
const Rights = () => import("views/power/Rights")
const Goods = () => import("views/goods/Goods")
const GoodsAdd = () => import("views/goods/GoodsAdd")
const Params = () => import("views/goods/Params")
const Categories = () => import("views/goods/Categories")
const Orders = () => import("views/orders/Orders")
const Reports = () => import("views/reports/Reports")


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    redirect: '/home/users',
    children: [
      {
        path: 'users',
        component: Users
      },
      {
        path: 'roles',
        component: Roles
      },
      {
        path: 'rights',
        component: Rights
      },
      {
        path: 'goods',
        component: Goods,
      },
      {
        path: 'goods/add',
        component: GoodsAdd
      },
      {
        path: 'params',
        component: Params
      },
      {
        path: 'categories',
        component: Categories
      },
      {
        path: 'orders',
        component: Orders
      },
      {
        path: 'reports',
        component: Reports
      }
    ]
  }
]


const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to是要访问的路径
  // from代表哪个路径跳转而来
  // next放行到下一步
  if(to.path == '/login') return next()
  // 获取token
  const tokenStr =  window.sessionStorage.getItem('token')
  if(!tokenStr) return next('/login')
  next()
})



export default router
